package cl.ccs.migracionbcmc.util;




import cl.ccs.migracionbcmc.util.Constants;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import javax.naming.InitialContext;




/**
 * Mail class for send mails.
 */
public final class Mail {
    //static Logger logger = Logger.getLogger(Mail.class);

    public Mail() {
        super();
    }

    public static boolean post(String from, String to, String subject, String messageTxt) {
      //  logger.info("Mail.post(String from, String to, String subject, String messageTxt):-->Inicio");
        InternetAddress ia;

        try {
            ia = new InternetAddress(from);
        } catch (Exception e) {
        //    logger.info(e.getMessage());
        //    logger.info(e);
            return false;
        }
        //logger.info("Mail.post(String from, String to, String subject, String messageTxt):-->Fin");
        return post(ia, to, subject, messageTxt);
    }

    public static boolean post(String from, String alias, String to, String subject, String messageTxt) {
        //logger.info("Mail.post(String from, String alias, String to, String subject, String messageTxt):-->Inicio");
        InternetAddress ia;

        try {
            ia = new InternetAddress(from, alias);
        } catch (Exception e) {
          //  logger.info(e.getMessage());
          //  logger.info(e);
            return false;
        }
       // logger.info("Mail.post(String from, String alias, String to, String subject, String messageTxt):-->Fin");
        return post(ia, to, subject, messageTxt);
    }

    private static boolean post(InternetAddress from, String to, String subject, String messageTxt) {
       // logger.info("Mail.post(InternetAddress from, String to, String subject, String messageTxt):-->Inicio");
        
        boolean sent;
        Message msg;
        MimeBodyPart mbp = new MimeBodyPart();
        Multipart mp = new MimeMultipart();
        Session session = getSession();

        sent = true;
        try {

            msg = new MimeMessage(session);
            msg.setFrom(from);
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            msg.setSubject(subject);

            mbp.setText(messageTxt);
            mp.addBodyPart(mbp);
            msg.setContent(mp);

            Transport.send(msg);

        } catch (Exception e) {
            sent = false;
         //   logger.info(e.getMessage());
         //   logger.info(e);
        }
        
     //   logger.info("Mail.post(InternetAddress from, String to, String subject, String messageTxt):-->Fin");
        return sent;
    }


    private static Session getSession() {
     //   logger.info("Mail.getSession:-->Inicio");
        Session mailSession;

        try {
            Authenticator authenticator;
            InitialContext ic;
            Session session;
            String user;
            String password;

            ic = new InitialContext();
            session = (Session)ic.lookup(Constants.MAIL_JNDI);

            user = session.getProperty(Constants.MAIL_USER_KEY);
            password = session.getProperty(Constants.MAIL_PASSWORD_KEY);

            authenticator = new SMTPAuthenticator(user, password);
            mailSession = Session.getInstance(session.getProperties(), authenticator);

        } catch (Exception e) {
            mailSession = null;
     //       logger.info(e.getMessage());
     //       logger.info(e);
        }
        
     //   logger.info("Mail.getSession:-->Fin");
        return mailSession;
    }
}
